using JGSY.CMS.LowCode.Platform.Application.DTOs;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JGSY.CMS.LowCode.Platform.Application.Interfaces
{
    /// <summary>
    /// 表单应用服务接口
    /// 提供表单管理相关的业务逻辑操作
    /// </summary>
    public interface IFormAppService
    {
        /// <summary>
        /// 获取所有表单
        /// </summary>
        /// <returns>表单列表</returns>
        Task<IEnumerable<FormDto>> GetAllAsync();

        /// <summary>
        /// 根据ID获取表单
        /// </summary>
        /// <param name="id">表单ID</param>
        /// <returns>表单信息</returns>
        Task<FormDto?> GetByIdAsync(long id);

        /// <summary>
        /// 根据标识符获取表单
        /// </summary>
        /// <param name="identifier">表单标识符</param>
        /// <returns>表单信息</returns>
        Task<FormDto?> GetByIdentifierAsync(string identifier);

        /// <summary>
        /// 获取活跃表单
        /// </summary>
        /// <returns>活跃表单列表</returns>
        Task<IEnumerable<FormDto>> GetActiveFormsAsync();

        /// <summary>
        /// 分页获取表单列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="keyword">搜索关键词</param>
        /// <param name="status">状态过滤</param>
        /// <returns>分页表单列表</returns>
        Task<(IEnumerable<FormDto> Items, int TotalCount)> GetPagedAsync(
            int pageIndex, 
            int pageSize, 
            string? keyword = null, 
            byte? status = null);

        /// <summary>
        /// 创建表单
        /// </summary>
        /// <param name="formDto">表单信息</param>
        /// <returns>创建的表单</returns>
        Task<FormDto> CreateAsync(FormDto formDto);

        /// <summary>
        /// 更新表单
        /// </summary>
        /// <param name="formDto">表单信息</param>
        /// <returns>更新的表单</returns>
        Task<FormDto> UpdateAsync(FormDto formDto);

        /// <summary>
        /// 删除表单
        /// </summary>
        /// <param name="id">表单ID</param>
        /// <returns>是否删除成功</returns>
        Task<bool> DeleteAsync(long id);

        /// <summary>
        /// 批量删除表单
        /// </summary>
        /// <param name="ids">表单ID列表</param>
        /// <returns>删除的数量</returns>
        Task<int> BatchDeleteAsync(IEnumerable<long> ids);

        /// <summary>
        /// 更新表单状态
        /// </summary>
        /// <param name="id">表单ID</param>
        /// <param name="status">状态</param>
        /// <returns>是否更新成功</returns>
        Task<bool> UpdateStatusAsync(long id, byte status);

        /// <summary>
        /// 复制表单
        /// </summary>
        /// <param name="id">原表单ID</param>
        /// <param name="newName">新表单名称</param>
        /// <param name="newIdentifier">新表单标识符</param>
        /// <returns>复制的表单</returns>
        Task<FormDto> DuplicateAsync(long id, string newName, string newIdentifier);

        /// <summary>
        /// 检查表单标识符是否存在
        /// </summary>
        /// <param name="identifier">表单标识符</param>
        /// <param name="excludeId">排除的表单ID</param>
        /// <returns>是否存在</returns>
        Task<bool> IdentifierExistsAsync(string identifier, long? excludeId = null);

        /// <summary>
        /// 提交表单数据
        /// </summary>
        /// <param name="formId">表单ID</param>
        /// <param name="data">提交数据</param>
        /// <param name="userAgent">用户代理</param>
        /// <param name="ipAddress">IP地址</param>
        /// <returns>提交结果</returns>
        Task<object> SubmitFormDataAsync(
            long formId, 
            Dictionary<string, object> data,
            string? userAgent = null,
            string? ipAddress = null);

        /// <summary>
        /// 获取表单提交数据
        /// </summary>
        /// <param name="formId">表单ID</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>提交数据列表</returns>
        Task<(IEnumerable<object> Items, int TotalCount)> GetFormSubmissionsAsync(
            long formId, 
            int pageIndex = 1, 
            int pageSize = 20);

        /// <summary>
        /// 导出表单提交数据
        /// </summary>
        /// <param name="formId">表单ID</param>
        /// <param name="format">导出格式（excel, csv等）</param>
        /// <returns>导出文件路径</returns>
        Task<string?> ExportFormSubmissionsAsync(long formId, string format = "excel");

        /// <summary>
        /// 获取表单统计信息
        /// </summary>
        /// <param name="formId">表单ID</param>
        /// <returns>统计信息</returns>
        Task<object> GetFormStatisticsAsync(long formId);

        /// <summary>
        /// 清理过期的表单提交数据
        /// </summary>
        /// <param name="daysToKeep">保留天数</param>
        /// <returns>清理的记录数</returns>
        Task<int> CleanupExpiredSubmissionsAsync(int daysToKeep = 365);

        /// <summary>
        /// 验证表单配置
        /// </summary>
        /// <param name="formDto">表单信息</param>
        /// <returns>验证结果</returns>
        Task<(bool IsValid, List<string> Errors)> ValidateFormConfigurationAsync(FormDto formDto);

        /// <summary>
        /// 预览表单
        /// </summary>
        /// <param name="id">表单ID</param>
        /// <returns>表单预览HTML</returns>
        Task<string?> PreviewFormAsync(long id);
    }
}
